AABB = class()

function AABB:init(x0,y0,z0,x1,y1,z1)
    self.x0 = x0
    self.y0 = y0
    self.z0 = z0
    self.x1 = x1
    self.y1 = y1
    self.z1 = z1
end

function AABB:move(x,y,z)
    self.x0 = self.x0 + x
    self.y0 = self.y0 + y
    self.z0 = self.z0 + z
    self.x1 = self.x1 + x
    self.y1 = self.y1 + y
    self.z1 = self.z1 + z
end

function AABB:expend(x,y,z)
    local _x0 = self.x0
    local _y0 = self.y0
    local _z0 = self.z0
    local _x1 = self.x1
    local _y1 = self.y1
    local _z1 = self.z1
    
    if(x<0) then _x0 = _x0 + x end
    if(x>0) then _x1 = _x1 + x end
    if(y<0) then _y0 = _y0 + y end
    if(y>0) then _y1 = _y1 + y end
    if(z<0) then _z0 = _z0 + z end
    if(z>0) then _z1 = _z1 + z end
    
    return AABB(_x0,_y0,_z0,_x1,_y1,_z1)
end

function AABB:grow(x,y,z)
    local _x0 = self.x0 - x
    local _y0 = self.y0 - y
    local _z0 = self.z0 - z
    local _x1 = self.x1 + x
    local _y1 = self.y1 + y
    local _z1 = self.z1 + z
    
    return AABB(_x0,_y0,_z0,_x1,_y1,_z1)
end

function AABB:clipXcollide(c,x)
    if ((c.y1 <= self.y0) or (c.y0 >= self.y1)) then return x end
    if ((c.z1 <= self.z0) or (c.z0 >= self.z1)) then return x end
    
    if ((x > 0.0) and (c.x1 <= self.x0)) then
      local max = self.x0 - c.x1
      if (max < x) then x = max end
    end
    if ((x < 0.0) and (c.x0 >= self.x1)) then
      local max = self.x1 - c.x0
      if (max > x) then x = max end
    end

    return x
end

function AABB:clipYcollide(c,y)
    if ((c.x1 <= self.x0) or (c.x0 >= self.x1)) then return y end
    if ((c.z1 <= self.z0) or (c.z0 >= self.z1)) then return y end

    if ((y > 0.0) and (c.y1 <= self.y0)) then
      local max = self.y0 - c.y1
      if (max < y) then y = max end
    end
    if ((y < 0.0) and (c.y0 >= self.y1)) then
      local max = self.y1 - c.y0
      if (max > y) then y = max end
    end

    return y
end

function AABB:clipZcollide(c,z)
    if ((c.y1 <= self.y0) or (c.y0 >= self.y1)) then return z end
    if ((c.x1 <= self.x0) or (c.x0 >= self.x1)) then return z end

    if ((z > 0.0) and (c.z1 <= self.z0)) then
      local max = self.z0 - c.z1
      if (max < z) then z = max end
    end
    if ((z < 0.0) and (c.z0 >= self.z1)) then
      local max = self.z1 - c.z0
      if (max > z) then z = max end
    end

    return z
end


function AABB:intesrsects(c)
    if ((c.x1 <= self.x0) or (c.x0 >= self.x1)) then return false end
    if ((c.y1 <= self.y0) or (c.y0 >= self.y1)) then return false end
    if ((c.z1 <= self.z0) or (c.z0 >= self.z1)) then return false end
    return true
end

function AABB:cloneMove(xa,ya,za)
    return AABB(self.x0 + xa,self.y0 + ya,self.z0 + za,self.x1 + xa,self.y1 + ya,self.z1 + za)
end
